package com.haoshuang.zhouzhoubang.ui.activity

import android.app.Activity
import android.content.Context
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ImageUtils
import com.haoshuang.zhouzhoubang.databinding.ActivityAgainDoorInBinding
import com.haoshuang.zhouzhoubang.ui.adapter.ChooseImageAdapter
import com.haoshuang.zhouzhoubang.ui.page.order.time.UpdateServiceTimeActivity
import com.haoshuang.zhouzhoubang.ui.vm.AgainDoorInViewModel
import com.haoshuang.zhouzhoubang.utils.MultiMediaChooseUtils
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.luck.picture.lib.entity.LocalMedia
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visibleOrGone


/**
 *  二次上门申请
 */
class AgainDoorInActivity : BaseVMActivity<AgainDoorInViewModel, ActivityAgainDoorInBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<AgainDoorInActivity>()
        }
    }

    private var service_day = ""
    private var service_time = ""

    // 二次上门费用
    private var second_visit_amount = ""
    private var cate_id = ""
    private var order_id = ""

    private var type = "1"

    override fun initView(savedInstanceState: Bundle?) {

        second_visit_amount = intent.getString("second_visit_amount")
        cate_id = intent.getString("cate_id")
        order_id = intent.getString("order_id")

        binding.titleBar.title = "二次上门"
        binding.titleBar.leftImageView().click { finish() }

        binding.rbCustomer.setOnCheckedChangeListener { compoundButton, b ->
            binding.llPrice.visibleOrGone(b)
            type = if (b){
                "2"
            }else{
                "1"
            }
        }

        binding.tvPrice.text = second_visit_amount

        binding.tvTime.clickWithTrigger {
            startPageForResult<UpdateServiceTimeActivity>("cate_id" to cate_id){code, data ->
                if (code == Activity.RESULT_OK){
                    if (data != null) {
                        service_day = data.getStringExtra("service_day").toString()
                        val service_day1 = data.getStringExtra("service_day1")
                        service_time = data.getStringExtra("service_time").toString()
                        binding.tvTime.text = "$service_day1     $service_time"
                    }
                }
            }
        }

        binding.tvConfirm.clickWithTrigger {
            val content = binding.etContent.text.toString()
            if (service_day.isEmpty() || service_time.isEmpty()) {
                toast("请选择二次上门时间")
                return@clickWithTrigger
            }
            if (content.isEmpty()) {
                toast("请如实填写二次上门原因")
                return@clickWithTrigger
            }
            val imageList = binding.recyclerView.getMediaList()
            if (imageList.isEmpty()) {
                toast("请选择图片")
                return@clickWithTrigger
            }
            vm.orderSecondVisit(order_id,
                service_day = service_day,
                service_time = service_time,
                content = content,
                image = imageList.joinToString(","){it.url.toString()},
                money = second_visit_amount,
                type = type, )
        }

    }

    override fun initData() {

    }

    override fun startObserve() {
        vm.orderSecondVisitData.observeState(this){
            onLoading {  }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (type == "1"){
                    // 个人原因
                    setResult(200)
                }else{
                    // 客户原因 需要前往支付
                    setResult(201)
                }

                finish()
            }
        }
    }
}